package com.example.qcweb;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.dao.DBDao;
import com.example.modal.CardInfo;
import com.example.qc.R;

import java.util.List;

public class QcardInfoActivity extends Activity {

    public class BtnClick implements OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
                case R.id.ibtn1:
                    Params params = new Params();
                    params.id = 1;
                    params.context = QcardInfoActivity.this;
                    new ProgressTask().execute(params);
                    break;
                case R.id.idaorubtn2:
                    Params params2 = new Params();
                    params2.id = 2;
                    params2.context = QcardInfoActivity.this;
                    new ProgressTask().execute(params2);
                default:
                    break;
            }

        }

    }

    public class Params {
        public int id;
        public Context context;
    }

    public static final int STATE_FINISH = 1;
    private static final int PROGESS_DIALOG = 1;
    private ProgressDialog m_Dialog = null;

    @Override
    @Deprecated
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        switch (id) {
            case PROGESS_DIALOG:
                m_Dialog = new ProgressDialog(QcardInfoActivity.this);
                m_Dialog.setMessage("正在加载，请稍侯...");
                m_Dialog.setCancelable(false);
                return m_Dialog;

            default:
                return null;
        }

    }

    public class ProgressTask extends AsyncTask<Params, Void, Integer> {

        @Override
        protected Integer doInBackground(Params... params) {
            // TODO Auto-generated method stub
            if (params[0].id == 1) {
                DataManager.imPortAllXmlFile(params[0].context);
                DBDao dbDao = new DBDao(QcardInfoActivity.this);
                list = dbDao.findAllCardInfo();

            } else if (params[0].id == 2) {
                DataManager.imPortNewXmlFile(params[0].context);
            }
            return STATE_FINISH;
        }

        @Override
        protected void onCancelled() {
            // TODO Auto-generated method stub
            super.onCancelled();
        }


        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            showDialog(PROGESS_DIALOG);
        }

        @Override
        protected void onPostExecute(Integer result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            int state = result.intValue();
            switch (state) {
                case STATE_FINISH:
                    dismissDialog(PROGESS_DIALOG);
                    Shelf_grid adapter = new Shelf_grid(QcardInfoActivity.this);
                    gv.setAdapter(adapter);
                    break;

                default:
                    break;
            }
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            // TODO Auto-generated method stub
            super.onProgressUpdate(values);
        }


    }

    List<CardInfo> list;
    GridView gv = null;

    public class ViewHolder {
        public TextView bianhao;
        public TextView tutuhao;
        public TextView mingcheng;
        public TextView xinghao;
        public TextView pici;
        // public Button btnButton;

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        // 无标题栏
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_qccardlist);
        gv = (GridView) findViewById(R.id.gridview);
        gv.setNumColumns(3);
        gv.setGravity(Gravity.CENTER);
        DBDao dbDao = new DBDao(QcardInfoActivity.this);
        list = dbDao.findAllCardInfo();
        Shelf_grid adapter = new Shelf_grid(this);
        gv.setAdapter(adapter);
        gv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                                    int position, long id) {
                MyGlobal.setcard(list.get(position).getKEYCardCode());
                Intent intent = new Intent(QcardInfoActivity.this,
                        QCCardViewActivity.class);
                // intent.putExtra("card", list.get(position).getKEYCardCode());

                startActivity(intent);
            }
        });
        ImageButton ibtn = (ImageButton) findViewById(R.id.ibtn1);
        ImageButton ibtn2 = (ImageButton) findViewById(R.id.idaorubtn2);
        ibtn2.setOnClickListener(new BtnClick());
        ibtn.setOnClickListener(new BtnClick());

	/*	class BtnClick implements OnClickListener{
			 public BtnClick() {
					super();
				}
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 Params Params=new Params();
					Params.id=1;
					Params.context=QcardInfoActivity.this;
					new ProgressTask().execute(Params);
			}
			
		}*/
        //	gv = (GridView) findViewById(R.id.gridview);

    }

    public class Shelf_grid extends BaseAdapter {
        private LayoutInflater inflater;

        public Shelf_grid(Context context) {
            super();
            inflater = LayoutInflater.from(context);


            // TODO Auto-generated constructor stub
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            if (null != list) {
                return list.size();
            } else {
                return 0;
            }
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            ViewHolder viewHolder;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_qcitem, null);
                viewHolder = new ViewHolder();
                viewHolder.bianhao = (TextView) convertView
                        .findViewById(R.id.qcardnumber);
                viewHolder.tutuhao = (TextView) convertView
                        .findViewById(R.id.drawingtnumber);
                viewHolder.mingcheng = (TextView) convertView
                        .findViewById(R.id.productname);
                viewHolder.xinghao = (TextView) convertView
                        .findViewById(R.id.productmodel);
                viewHolder.pici = (TextView) convertView
                        .findViewById(R.id.productbatch);
                // viewHolder.btnButton=(Button)convertView.findViewById(R.id.btn1);
                // array=new String[list.size()];
				/*
				 * for(int i = 0;i<list.size();i++){ CardInfo cardInfo =
				 * list.get(i);
				 * 
				 * viewHolder.bianhao=cardInfo.getKEYCardCode();
				 * viewHolder.tutuhao=cardInfo.getDrawingNumber();
				 * viewHolder.mingcheng=cardInfo.getProductName();
				 * viewHolder.xinghao=cardInfo.getProductModel();
				 * viewHolder.pici=cardInfo.getProductBatchNumber(); }
				 */
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.bianhao.setText(list.get(position).getKEYCardCode());
            viewHolder.tutuhao.setText(list.get(position).getDrawingNumber());
            viewHolder.mingcheng.setText(list.get(position).getProductName());
            viewHolder.xinghao.setText(list.get(position).getProductModel());
            viewHolder.pici.setText(list.get(position).getProductBatchNumber());
            // viewHolder.image.setImageResource(list.get(position).getImageId());
            Log.i("MainActivity", "<<<<<<<" + position);
            return convertView;
        }
    }

}
